package cn.marinda.gui.impl;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.config.AbstractServerConfiguration;
import cn.marinda.entity.Server;
import cn.marinda.entity.ServerCore;
import cn.marinda.entity.ServerInfo;
import cn.marinda.entity.ServerPlugin;
import cn.marinda.factory.ServerConfigFactory;
import cn.marinda.gui.AbstractServerDialog;
import cn.marinda.runnable.ServerImmitConfigRunnable;
import cn.marinda.util.HttpUtils;
import cn.marinda.util.ServerUtils;
import cn.marinda.util.ThreadUtils;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

/**
 * 下载的提示框
 *
 * @author marinda
 * @date 2022/07/14
 */
@Data
public class DownloadServerDiaLogImpl extends AbstractServerDialog {

    private Set<String> pluginList = new HashSet<>();

    public static DownloadServerDiaLogImpl instance = new DownloadServerDiaLogImpl();
    private String serverCore;
    private JProgressBar progressBar;
    private ServerInfo serverInfo;


    /**
     * 下载的提示框
     */
    public DownloadServerDiaLogImpl(String serverCore, Set<String> pluginList, Map<String,Boolean> radioData, Map<String,String> textData){
        super();
        this.serverCore = serverCore;
        this.pluginList = pluginList;
        this.radioData = radioData;
        this.textData = textData;
        this.serverInfo = MinecraftServerHelper.serverInfo;
        HttpUtils.isOnlineCore = false;
        downloadServerCore();
        HttpUtils.isOnlineFlag = false;
        downloadServerPlugin();
        immitServerInfo();

    }

    /**
     * 下载的提示框
     */
    public DownloadServerDiaLogImpl(Set<String> pluginList){
        super();
        this.pluginList = pluginList;
        this.serverInfo = MinecraftServerHelper.serverInfo;
        HttpUtils.isOnlineFlag = true;
        downloadServerPlugin();
    }

    /**
     * 下载的提示框
     */
    public DownloadServerDiaLogImpl(String serverCoreName){
        super();
        this.serverInfo = MinecraftServerHelper.serverInfo;
        HttpUtils.isOnlineCore = true;
        downloadServerCore();
    }

    /**
     * 下载服务器核心
     */
    private void downloadServerCore(){
        ServerCore serverCore = new ServerCore(this.serverCore);
        serverCore.download(progressBar);
    }


    /**
     * 下载服务器插件
     */
    private void downloadServerPlugin(){
        ServerPlugin plugin = new ServerPlugin();
        plugin.download(progressBar);
    }

    /**
     * 注入服务器信息
     */
    private void immitServerInfo(){
        DownloadServerDiaLogImpl.instance.close();
         ServerImmitConfigRunnable serverImmitConfigRunnable = new ServerImmitConfigRunnable(radioData,textData,serverInfo);
        ThreadUtils.serverImmitScheduled = MinecraftServerHelper.executorService.submit(serverImmitConfigRunnable);
    }


    /**
     * 分割插件列表
     *
     * @return {@link List}<{@link String}>
     */
    private String splitPluginList(String target){
        String result = "";
//        检索开头字符
        if(target.startsWith("[")){
            int startIndex = target.indexOf("]")+1;
            int endIndex = target.length();
            result = target.substring(startIndex,endIndex);
        }
        return result;
    }

    private void filterString(Set<String> setList,String filterFileName){
        setList.add(filterFileName);
    }



    private DownloadServerDiaLogImpl(){
        super();
    }

    @Override
    protected JDialog loadDialog() {
        dialog = new JDialog();
        dialog.setSize(300,80);
        dialog.setTitle("下载提示框");
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setResizable(false);
        dialog.setContentPane(view);
        return dialog;
    }

    @Override
    protected JPanel loadView() {
        view  = new JPanel(new FlowLayout(FlowLayout.CENTER));
        Stream<JComponent> componentStream = componentList.stream();
        componentStream.forEach(component -> {
            view.add(component);
        });

        return view;
    }

    @Override
    protected List<JComponent> loadComponent() {
        JLabel processLabel = new JLabel("当前进度");
        processLabel.setFont(new Font(null, Font.PLAIN,14));

//        最小零，最大一百
        JProgressBar progressBar = new JProgressBar(0,100);
        progressBar.setStringPainted(true);
        this.progressBar = progressBar;

        componentList.add(processLabel);
        componentList.add(progressBar);
        return componentList;
    }


    /**
     * 开放
     */
    @Override
    public void open() {
        dialog.setVisible(true);
    }

    /**
     * 关闭
     */
    @Override
    public void close() {
        dialog.setVisible(false);
    }
}
